package do2

import (
	"github.com/go-ini/ini"
	"log"
	"time"
)

type App struct {
	JwtSecret string
	PageSize  int
	PrefixUrl string

	RuntimeRootPath string

	ImageSavePath  string
	ImageMaxSize   int
	ImageAllowExts []string

	ExportSavePath string
	QrCodeSavePath string
	FontSavePath   string

	LogSavePath string
	LogSaveName string
	LogFileExt  string
	TimeFormat  string

	Timezone string
	LogLevel string
	LogJSON  bool
	LogFile  string
}

var AppSetting = &App{}

type Server struct {
	RunMode      string
	HttpPort     int
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

var ServerSetting = &Server{}

type Idv struct {
	ProgramPath string
	Parameter   string
}

var IdvSetting = &Idv{}

type Ldm struct {
	ProgramPath string
	Parameter   string
}

var LdmSetting = &Ldm{}

type Tds struct {
	ProgramPath   string
	TdsFileServer string
}

var TdsSetting = &Tds{}

type Unzip struct {
	DataPath string
}

var UnzipSetting = &Unzip{}

type Kafka struct {
	IpAddr string
	Topic  string
}

var KafkaSetting = &Kafka{}

type Redis struct {
	Mode     string
	IpAddr   []string
	Password string
	DB       int
	PoolSize int
}

var RedisSetting = &Redis{}

var cfg *ini.File

func Setup() {
	var err error
	cfg, err = ini.Load("D:\\gitee\\golang_project\\ffmpeg\\conf\\app.ini")
	//cfg, err = ini.Load("D:\\app.ini")
	if err != nil {
		log.Fatalf("do2.Setup, fail to parse 'conf/app.ini': %v", err)
	}

	mapTo("app", AppSetting)
	mapTo("server", ServerSetting)
	mapTo("idv", IdvSetting)
	mapTo("ldm", LdmSetting)
	mapTo("wis-tds", TdsSetting)
	mapTo("unzip", UnzipSetting)
	mapTo("kafka", KafkaSetting)
	mapTo("redis", RedisSetting)

	AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
	ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
	ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second

	log.Printf("配置文件加载完成.")
}

func mapTo(section string, v interface{}) {
	err := cfg.Section(section).MapTo(v)
	if err != nil {
		log.Fatalf("Cfg.MapTo %s err: %v", section, err)
	}

}
